package ru.javadevelop.vegetarian.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import ru.javadevelop.vegetarian.R;
import ru.javadevelop.vegetarian.common.ExtApplication;
import ru.javadevelop.vegetarian.common.ImageManager;
import ru.javadevelop.vegetarian.common.ScreenUtils;
import ru.javadevelop.vegetarian.common.interfaces.IActivity;
import ru.javadevelop.vegetarian.controls.HeaderTitles;
import ru.javadevelop.vegetarian.fragments.HeaderFragment;
import ru.javadevelop.vegetarian.fragments.RecipesFragment;
import ru.javadevelop.vegetarian.model.Favorite;
import ru.javadevelop.vegetarian.model.Image;

import java.sql.SQLException;
import java.util.ArrayList;

public class RecipeDetailsActivity extends Activity implements IActivity {
    private LinearLayout root;
    private LinearLayout details_container;
    private HeaderFragment header_fragment;
    private TextView ingredients;
    private TextView process;
    private TextView add_to_fav;


    //******************************************************************************************************************
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe_details);
        findControls();
        fillControls();
        setListeners();
    }

    @Override
    public void findControls() {
        root = (LinearLayout)findViewById(R.id.root);
        header_fragment = (HeaderFragment)getFragmentManager().findFragmentById(R.id.header_fragment);
        ingredients = (TextView)findViewById(R.id.ingredients);
        process = (TextView)findViewById(R.id.process);
        details_container = (LinearLayout)findViewById(R.id.details_container);
        add_to_fav = (TextView)findViewById(R.id.add_to_fav);
    }

    @Override
    public void fillControls() {
        HeaderTitles headerTitles = new HeaderTitles(
                new Intent(this, RecipesInCatActivity.class),
                ExtApplication.Session.recipe.getCategory().name,
                ExtApplication.Session.recipe.name
        );
        header_fragment.setHeaderTitles(headerTitles);
        ingredients.setText(ExtApplication.Session.recipe.ingredients);
        process.setText(ExtApplication.Session.recipe.process);
        ArrayList<Image> images =  ExtApplication.Session.recipe.getImages();
        if ( (images!=null) && (images.size()>0))
        {
            for (Image image:images)
            {
                details_container.addView(createSeparator());
                View imageview = createImageView(image);
                if (imageview!=null)
                {
                    details_container.addView(imageview);
                }
            }

        }
        setFavText();


    }
    //******************************************************************************************************************
    private View createImageView(Image image)
    {
        ImageManager im = new ImageManager(this, 800, 700);
        Bitmap bm = im.setIsScale(true)
                .setIsResize(true)
                .setIsCrop(true)
                .getFromFile(image.getFilePath(this));

//        BitmapFactory.Options option = new BitmapFactory.Options();
//        option.inSampleSize = 2;
//        Bitmap myBitmap = BitmapFactory.decodeFile(image.getFilePath(RecipeDetailsActivity.this), option);
        if (bm==null)
            return null;
        ImageView imageView = new ImageView(this);

        int width = this.getResources().getDisplayMetrics().widthPixels - 30;
        if (width>800) width = 800;
        int height = width*bm.getHeight()/bm.getWidth();
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
        params.setMargins(0, 15, 0, 0);
        imageView.setLayoutParams(params);

        imageView.setMaxWidth(800);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageBitmap(bm);

        return imageView;
    }
    //******************************************************************************************************************
    private View createSeparator()
    {
        TextView layout = new TextView(this);
        ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(
                100, 15);
        return  layout;
    }
    //******************************************************************************************************************
    @Override
    public void setListeners() {
        add_to_fav.setOnClickListener(new OnAddToFavListener());
    }
    //******************************************************************************************************************
    private class OnAddToFavListener implements View.OnClickListener
    {

        @Override
        public void onClick(View v) {
            Favorite favorite = Favorite.get(
                    ExtApplication.Session.googleAccount,
                    ExtApplication.Session.recipe.id
            );
            if ((favorite==null)||(favorite.isDeleted))
            {
                favorite = new Favorite(
                        ExtApplication.Session.googleAccount,
                        ExtApplication.Session.recipe.id,
                        false
                );
                try {
                    favorite.save();
                    RecipeDetailsActivity.this.setFavText();
                } catch (SQLException e) {
                    throw  new RuntimeException(e);
                }
            }
            else
            {
                try {
                    favorite.isDeleted = true;
                    favorite.save();
                    RecipeDetailsActivity.this.setFavText();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    //******************************************************************************************************************
    private void setFavText() {
        Favorite favorite = Favorite.get(
                ExtApplication.Session.googleAccount,
                ExtApplication.Session.recipe.id
        );
        if ((favorite!=null)&& (!favorite.isDeleted))
        {
            add_to_fav.setText(R.string.delete_from_fav);
            add_to_fav.setCompoundDrawablesWithIntrinsicBounds(null,
                    null,
                    getResources().getDrawable(R.drawable.img_no_star_small),
                    null);
        }
        else
        {
            add_to_fav.setText(R.string.add_to_fav);
            add_to_fav.setCompoundDrawablesWithIntrinsicBounds(null,
                    null,
                    getResources().getDrawable(R.drawable.img_star_small),
                    null);
        }
    }
    //******************************************************************************************************************
    //******************************************************************************************************************
}

